using Arcane.Source.Utils;
using Godot;

namespace Arcane.Source.Data
{
    public class GameDB
    {
        readonly static string SETTINGS_PATH = "user://settings.dat";

        private static GameDB _inst = null;
        public static GameDB Instance 
        {
            get
            {
                _inst ??= new();
                return _inst;
            }
        }
        public static bool Initiated { get; private set; }

        private readonly TextureDB texture = new();
        public static TextureDB Texture => Instance.texture;

        private readonly ElementDB element = new();
        public static ElementDB Element => Instance.element;


        private readonly StatusEffectDB statusEffect = new();
        public static StatusEffectDB StatusEffect => Instance.statusEffect;

        private SettingsData settings = new();
        public static ref SettingsData Settings => ref Instance.settings;

        private readonly CharacterDB character = new();
        public static CharacterDB Character => Instance.character;

        private readonly SpellDB spell = new();
        public static SpellDB Spell => Instance.spell;

        private readonly RuneDB rune = new();
        public static RuneDB Rune => Instance.rune;

        public static void Init() => Instance.Initiate();
        public static void Deinit() => Instance?.Deinitiate();

        private void Initiate()
        {
            if (Initiated) return;

            texture.Load("res://resource/texture");

            element.Init("res://resource/data/element");

            statusEffect.Init(texture);

            spell.Init(texture, "res://resource/data/spell");
            rune.Init("res://resource/data/rune");

            character.Load("res://resource/data/chara");


            Initiated = true;
        }

        private void Deinitiate()
        {
            settings.Save(SETTINGS_PATH);
        }
    }
}